package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类表(Category)表控制层
 *
 * @author makejava
 * @since 2024-01-13 17:16:25
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    /**
     * 服务对象
     */
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取需要输入建议的商品分类
     *
     * @return
     */
    @GetMapping("/loadAllCategory")
    public Result loadAllQuerySearch() {
        return Result.success(categoryService.loadAllCategory());
    }


    /**
     * 根据ID修改状态
     *
     * @param id
     * @param status
     */
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestParam("id") Integer id,
                               @RequestParam("status") Integer status) {
        categoryService.updateStatus(id, status);
        return Result.success("修改状态成功！");
    }

    /**
     * 获取所有分类信息
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        return Result.success(categoryService.selectAll());
    }

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        categoryService.deleteById(id);
        return Result.success("删除成功!");
    }

    /**
     * 更新分类信息
     *
     * @param category
     * @return
     */
    @PutMapping("/updateById")
    public Result updateById(@RequestBody Category category) {
        categoryService.updateById(category);
        return Result.success("更新成功！");
    }


    /**
     * 条件查询  分页
     *
     * @param currentPage
     * @param pageSize
     * @param name
     * @param detail
     * @param status
     * @return
     */
    @GetMapping("/selectByManyConditionInpage")
    public Result selectByManyConditionInpage(@RequestParam("currentPage") Integer currentPage,
                                              @RequestParam("pageSize") Integer pageSize,
                                              @RequestParam("name") String name,
                                              @RequestParam("detail") String detail,
                                              @RequestParam("status") Integer status) {
        return Result.success(categoryService.selectByManyConditionInpage(currentPage, pageSize, name, detail, status));
    }

    /**
     * 条件查询  分页
     *
     * @param currentPage
     * @param pageSize
     * @param name
     * @param detail
     * @return
     */
    @GetMapping("/selectByManyConditionNoStatusInpage")
    public Result selectByManyConditionNoStatusInpage(@RequestParam("currentPage") Integer currentPage,
                                                      @RequestParam("pageSize") Integer pageSize,
                                                      @RequestParam("name") String name,
                                                      @RequestParam("detail") String detail) {
        return Result.success(categoryService.selectByManyConditionNoStatusInpage(currentPage, pageSize, name, detail));
    }

    /**
     * 分页查询
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam("currentPage") Integer currentPage,
                               @RequestParam("pageSize") Integer pageSize) {
        return Result.success(categoryService.selectByPage(currentPage, pageSize));
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        categoryService.deleteByIds(ids);
        return Result.success("删除成功！");
    }


    /**
     * 添加分类
     *
     * @param category
     * @return
     */
    @PostMapping("/addCategory")
    public Result addCategory(@RequestBody Category category) {
        categoryService.addCategory(category);
        return Result.success("添加成功！");
    }


    /**
     * 分类详情
     *
     * @param id
     * @return
     */
    @GetMapping("/categoryInfo")
    public Result categoryInfo(@RequestParam("id") Integer id) {
        return Result.success(categoryService.selectById(id));
    }


}

